/**
 * Copyright (c) Software Institute of PKU
 * All rights reserved. 
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 */
package edu.pku.sei.modeler.editparts;

import java.beans.PropertyChangeEvent;
import java.util.Iterator;
import java.util.List;

import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.Label;
import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.editpolicies.DirectEditPolicy;
import org.eclipse.gef.requests.DirectEditRequest;

import edu.pku.sei.modeler.editpolicies.ClassNodeEditPolicy;
import edu.pku.sei.modeler.figures.InstanceSpecificationFigure;
import edu.pku.sei.modeler.model.structure.MetaNode;
import edu.pku.sei.modeler.model.structure.MetaShapeElement;
import edu.pku.sei.modeler.model.uml.Kernel.Classifier;
import edu.pku.sei.modeler.model.uml.Kernel.InstanceSpecification;
import edu.pku.sei.modeler.uml.commands.ChangeNameCommand;

/**
 * @generated
 *
 */
public class InstanceSpecificationEditpart extends AbstractUMLNodeEditPart 
{
	public InstanceSpecificationEditpart (MetaShapeElement model) {
		super(model);
	}
	/* (non-Javadoc)
	 * @see org.eclipse.gef.editparts.AbstractGraphicalEditPart#createFigure()
	 */
	protected IFigure createFigure() {
		return new InstanceSpecificationFigure();
	}

	protected DirectEditPolicy createDirectEditPolicy() {
		return new DirectEditPolicy() {
			protected Command getDirectEditCommand(DirectEditRequest request) {
				return new ChangeNameCommand(getModelElement(), (String) request.getCellEditor().getValue());
			}

			protected void showCurrentEditValue(DirectEditRequest request) {
				IFigure fig = getDirectEditFigure();
				if (fig instanceof Label) {
					((Label) fig).setText(getModelElement().getName());
					fig.getUpdateManager().performUpdate();
				}
			}
		};
	}
	
	
	@Override
	protected IFigure getDirectEditFigure() {
		return ((InstanceSpecificationFigure) this.getFigure()).getNameLabel();
	}

	/* (non-Javadoc)
	 * @see org.eclipse.gef.editparts.AbstractEditPart#createEditPolicies()
	 */
	protected void createEditPolicies() {
		super.createEditPolicies();
		installEditPolicy(EditPolicy.GRAPHICAL_NODE_ROLE, new ClassNodeEditPolicy());
	}

	protected void handlePropertyChanged(PropertyChangeEvent event) {
		refreshVisuals();
		super.handlePropertyChanged(event);
	}
	
	private InstanceSpecification getModelElement() {
		return (InstanceSpecification)((MetaNode)getModel()).getModelElement();
	}
	
	@SuppressWarnings("unchecked")
	protected void refreshVisuals() {
		super.refreshVisuals();
		InstanceSpecification modelElement = getModelElement();
		InstanceSpecificationFigure figure = (InstanceSpecificationFigure)getFigure();
		
		//set name label
		List classifiers = modelElement.getClassifier();
		if (classifiers.isEmpty()) {
			figure.setName(modelElement.getName() + " :<Undefined>");
		}
		else {
			String type = "";
			for (Iterator<Classifier> iter = classifiers.iterator(); iter.hasNext();) {
				type = type + iter.next().getName()+ ", ";
			}
			
			type = type.substring(0, type.length() - 2);
			figure.setName(modelElement.getName() + " : " + type);
		}
		

//		figure.getNameLabel().setIcon(ImageProvider.getImage(ImageProvider.INSTANCESPECIFICATION_IMAGE));
//		figure.getNameLabel().setIconAlignment(PositionConstants.TOP);
//		figure.getNameLabel().setTextAlignment(PositionConstants.CENTER);
		
	}
}
